package com.example.ebeanbase.common.base;

import com.example.ebeanbase.entity.User;
import com.example.ebeanbase.service.UserService;
import com.example.ebeanbase.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Component
public class BaseController {
    @Autowired
    private HttpServletRequest request;

    public static User userInfo;

    @Autowired
    protected UserService userService;

    private static BaseController serverHandler;

    @PostConstruct // 通过@PostConstruct实现初始化bean之前进行的操作
    public void init() {
        serverHandler = this;
        serverHandler.userService = this.userService;
        // 初使化时将已静态化的testService实例化
    }

    /**
     * 获取token
     *
     * @return
     */
    protected String getToken() {
        return request.getHeader("token");
    }

    /**
     * 获取角色类型
     *
     * @return
     */
    protected String getRoleType() {
        return request.getHeader("roleType");
    }

    /**
     * 获取用户信息
     *
     * @param
     * @return
     */
    public User getUserInfo(String token, String roleType) {

        // 获取session信息
        User sessionInfo = serverHandler.userService.selectUserByToken(token,roleType);

       if(StringUtil.isNotBlank(sessionInfo)){
           return sessionInfo;
       }
       return null;
    }

    /**
     * 验证参数是否合法
     *
     * @param result
     */
    public static void checkParam(BindingResult result) {

        if (result.hasErrors()) {
            result.getAllErrors().stream().forEach(error -> {
                FieldError fieldError = (FieldError) error;
                throw new RuntimeException(fieldError.getDefaultMessage());
            });
        }
    }

    //参数type无效，为了兼容，先保留
    public String getWrapperView(String viewName , String prefix, HttpServletRequest request){
        String relatedPath = prefix + "/";
        HttpSession session = request.getSession();
        Object terminal = session.getAttribute("terminal");
        //根据终端参数，决定是否修改type值
        //1-PC终端；2-移动终端；3-微信
        int type = 1; //默认为pc
        if(terminal != null){
            switch (terminal.toString().toLowerCase()) {
                case "pc":
                    type = 1;
                    break;
                case "mb":
                    type = 2;
                    break;
                case "wx":
                    type = 3;
                    break;
                default:
                    break;
            }
        }
        //根据type指向不同路径
        String partitioningPath = new String();
        switch(type){
            case 1:
                partitioningPath = "pc/";
                break;
            case 2:
                partitioningPath = "mb/";
                break;
            case 3:
                partitioningPath = "wx/";
                break;
            default:
                partitioningPath = "pc/";
                break;
        }

        relatedPath += partitioningPath + viewName;
        return relatedPath;
    }

    /**
     * 获取用户id
     *
     * @return
     */
   /* public int getUserId() {

        int userId = 0;

        if (userInfo != null) {

            userId = userInfo.getId();
        }

        return userId;
    }*/

}
